package com.chtw.oasystem.service.impl;

import com.chtw.oasystem.bean.ChangeLogs;
import com.chtw.oasystem.bean.ChangeLogsExample;
import com.chtw.oasystem.bean.FullUserInfo;
import com.chtw.oasystem.mapper.ChangeLogsMapper;
import com.chtw.oasystem.respBean.ListChangeLogsResp;
import com.chtw.oasystem.respBean.Logs;
import com.chtw.oasystem.service.ChangeLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chtw
 * @date 2020-11-17 22:00
 */
@Service
public class ChangeLogServiceImpl implements ChangeLogService {
    @Autowired
    private ChangeLogsMapper changeLogsMapper;
    private static final Logger logger = LoggerFactory.getLogger(ChangeLogServiceImpl.class);

    @Autowired
    private DeptServiceImpl deptService;

    @Autowired
    private UsersServiceImpl usersService;

    @Override
    public void saveChangeLog(ChangeLogs changeLogs) {
        try {
            changeLogsMapper.insert(changeLogs);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw e;
        }
    }

    @Override
    public PageInfo<ChangeLogs> getChangeLogs(int page, int size, String search, FullUserInfo currentUser) {
        try {
            PageHelper.startPage(page, size);
            String deptId = currentUser.getDeptId();
            List<String> ownerDeptIds = deptService.getOwnerDeptIds(deptId);
            List<String> OwnerUserNames = usersService.getOwnerUserNames(ownerDeptIds);
            List<ChangeLogs> logs = new ArrayList<>();
            for (String ownerUserName : OwnerUserNames) {
                ChangeLogsExample example = new ChangeLogsExample();
                example.or().andOperatorLike("%" + search + "%");
                example.or().andOperationLike("%" + search + "%");
                example.or().andOperationEqualTo(ownerUserName);
                List<ChangeLogs> log = changeLogsMapper.selectByExample(example);
                logs.addAll(log);
            }
            return new PageInfo<>(logs);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public ListChangeLogsResp list(int page, int size, FullUserInfo currentUser) {
        String deptId = currentUser.getDeptId();
        List<String> ownerDeptIds = deptService.getOwnerDeptIds(deptId);
        List<String> OwnerUserNames = usersService.getOwnerUserNames(ownerDeptIds);
        OwnerUserNames.add(currentUser.getUserName());
        // 获取日志
        int offset = (page - 1) < 0 ? 0 : (page - 1) * size;
        List<ChangeLogs> logs = changeLogsMapper.selectLogs(size, offset, OwnerUserNames);
        List<Logs> ls = new ArrayList<>();
        for (ChangeLogs log : logs) {
            ls.add(new Logs(log.getOperator(), log.getOperation(), log.getOperationTime()));
        }
        // 获取页码总数
        int count = changeLogsMapper.countLog(OwnerUserNames);
        int page_total = count % size == 0 ? count / size : count / size + 1;
        return new ListChangeLogsResp(ls, count, page_total);
    }
}
